package com.inet.test07;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author weijingchen
 * @Date 2025/6/26 15:15
 * @Version 1.0.0
 */
public class Server {

    static List<Socket> socketList=new ArrayList<>();
    public static void main(String[] args) throws IOException {


        ThreadPoolExecutor pool = new ThreadPoolExecutor(5,
                10,
                1000,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        //获取本地文件中的用户名和密码

        List<User> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("src/main/resources/f.txt"))));
        String line;
        while ((line = br.readLine()) != null) {
            String[] split = line.split("=");
            User user = new User(split[0], split[1]);
            list.add(user);
        }
        br.close();
        System.out.println(list);

        ServerSocket ss = new ServerSocket(10301);


        while (true) {
            Socket socket = ss.accept();
            System.out.println("有一个客户端连接成功");
            //开启一条线程
            pool.submit(new MyRunnable(socket,list));
        }
    }
}
